﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
using System.Web.Helpers;

public static class Helpers
{ 
    public static IHtmlString Youtube(string link, int width = 480, int height = 385, bool allowFullScreen = true)
    {
        string innerHtml = string.Empty;
        var url = "http://www.youtube.com/v/{0}?fs=1&hl=en_US";
        
        //prepare video embed URL
        var uri = new Uri(link);
        var querystring = uri.Query;
        var videoCode = HttpUtility.ParseQueryString(querystring).Get("v");
        
        url = string.Format(url, videoCode);
        
        //prepare HTM
        var html = new TagBuilder("object");
        html.MergeAttribute("width", width.ToString(), true);
        html.MergeAttribute("height", height.ToString(), true);
        
        var param1 = new TagBuilder("param"); param1.MergeAttribute("name", "movie", true);
        param1.MergeAttribute("value", url, true);   var param2 = new TagBuilder("param");
        param2.MergeAttribute("name", "allowFullScreen", true);
        param2.MergeAttribute("value", allowFullScreen.ToString(), true);
        
        var param3 = new TagBuilder("param"); param3.MergeAttribute("name", "allowscriptaccess", true);
        param3.MergeAttribute("value", "always", true);
        
        var embed = new TagBuilder("embed"); embed.MergeAttribute("src", url, true);
        embed.MergeAttribute("type", "application/x-shockwave-flash", true);
        embed.MergeAttribute("allowscriptaccess", "always", true);
        embed.MergeAttribute("allowfullscreen", allowFullScreen.ToString(), true);
        embed.MergeAttribute("width", width.ToString(), true);
        embed.MergeAttribute("height", height.ToString(), true);
        
        innerHtml += param1.ToString(TagRenderMode.SelfClosing);
        innerHtml += param2.ToString(TagRenderMode.SelfClosing);
        innerHtml += param3.ToString(TagRenderMode.SelfClosing);
        innerHtml += embed.ToString(TagRenderMode.SelfClosing);
        
        html.InnerHtml = innerHtml;
        
        return new HtmlString(html.ToString(TagRenderMode.Normal));
    }
}
